package com.lzh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.pojo.CT;
import com.lzh.pojo.Class;
import com.lzh.pojo.Student;
import com.lzh.pojo.Teacher;
import com.lzh.service.CTService;
import com.lzh.service.StudentService;
import com.lzh.service.TeacherService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author reborn
 * @create 2020/5/21-23:46
 * 中途修改过xml配置，查询班级以前I全部查询再在前端遍历得到，现在添加list的id集合查询，以前的就没优化；
 */
@Controller
public class CTController {
    @Autowired
    CTService ctService;

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    //    前往修改学生页面
    @RequestMapping("/toUpd")
    public String  toUpdStu(int id,Model model){
        List<Class> classes = ctService.selectCname(null);
       Student student = studentService.selectStuById(id);
            model.addAttribute("student",student);
            System.out.println(student);
        model.addAttribute("classes",classes);
        model.addAttribute("type","upd");
        return "student/addStu";
    }

//    前往添加学生页面
    @RequestMapping("/toAdd")
    public String  test7(int id,Model model){
        List<Integer> integers = ctService.selectCidByTid(id);
        List<Class> classes = ctService.selectCname(integers);
        List<Class> classes1 = ctService.selectCname(null);
        model.addAttribute("classes",classes);
        model.addAttribute("classes1",classes1);
        model.addAttribute("tid",integers);
        model.addAttribute("type","add");
        return "student/addStu";
    }

//    查询我的学生
    @RequestMapping("/selStu")
    public String selectStuByTid(@RequestParam(required = true,value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(required = true,value = "pageSize",defaultValue = "5")Integer pageSize,
                                 int id, Model model){
        List<Integer> cts = ctService.selectCidByTid(id);
        List<Class> classes = ctService.selectCname(null);
        PageInfo<Student> students = studentService.selectStuAll(pageNum,pageSize,cts);
        System.out.println(students);
        model.addAttribute("pageInfoL",students);
        model.addAttribute("cname",classes);
        //        设置标记

        model.addAttribute("bj",1);
        return "student/student";
    }

    //    根据教师id查询这个老师的所有学生
    @RequestMapping("/selOStu")
    public String selectStuByOTid(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                                 int id, Model model){
        List<Integer> cts = ctService.selectCidByTid(id);
        List<Class> classes = ctService.selectCname(null);
        PageInfo<Student> students = studentService.selectStuAll(pageNum,pageSize,cts);
        System.out.println(students);
        model.addAttribute("pageInfoL",students);
        model.addAttribute("cname",classes);
        //        设置标记
        model.addAttribute("bj",4);
        return "student/student";
    }

    //查询所有学生
    @RequestMapping("/allStu")
    public String  selectStu(@RequestParam(required = true,value = "pageNum",defaultValue = "1")Integer pageNum,
                             @RequestParam(required = true,value = "pageSize",defaultValue = "5")Integer pageSize,
                            Model model){

        PageInfo<Student> students = studentService.selectStus(pageNum,pageSize);
        System.out.println(students);
        List<Class> classes = ctService.selectCname(null);
        model.addAttribute("cname",classes);
        model.addAttribute("pageInfoL",students);
//       ctService.selectTidByCid()
        //        设置标记
        model.addAttribute("bj",2);
        return "student/student";
    }


    //    显示学生个人信息
    @RequestMapping("/stuMe")
    public String TeaMessage(int id,Model model){
        Student student = studentService.selectStuById(id);
        model.addAttribute("student",student);

        List<Class> classes = ctService.selectCname(Arrays.asList(student.getClassId()));
            model.addAttribute("cname",classes);

        List<CT> cts = ctService.selectTeaByCid(student.getClassId());
        model.addAttribute("cts",cts);
        return "student/stuOne";
    }

    //插入学生 ,然后返回新插入的学生信息
    @RequestMapping("/insertStu")
    public String insertStu(Student student,Model model){
        studentService.insertStu(student);
        model.addAttribute("iud","添加");
        List<Class> classes = ctService.selectCname(null);
        model.addAttribute("cname",classes);
            model.addAttribute("student1",student);
        return "student/student";
    }


//    根据id删除学生
    @RequestMapping("/delStu")
    public String deleteStu(int id,Model model){

        System.out.println(id);
        Student student = studentService.selectStuById(id);
            model.addAttribute("student1",student);
        List<Class> classes = ctService.selectCname(null);
        model.addAttribute("cname",classes);
        model.addAttribute("iud","删除");
        studentService.deleteStu(id);
        return "student/student";
    }

//    更新学生
    @RequestMapping("/updateStu")
    public String updateStu(Student student,Model model){
        studentService.updateStu(student);
       Student students = studentService.selectStuById(student.getId());
            model.addAttribute("student1",student);
        List<Class> classes = ctService.selectCname(null);
        model.addAttribute("cname",classes);
        model.addAttribute("iud","更新");
        return "student/student";
    }

    //根据名字查询
    @RequestMapping("/selStuByName")
    public String selectStuByName(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                  String name, Model model){
        PageInfo<Student> studentPageInfo = studentService.selectStuByName(pageNum, pageSize, name);
        model.addAttribute("pageInfoL",studentPageInfo);
        List<Class> classes = ctService.selectCname(null);

        model.addAttribute("name1",name);
        System.out.println(name);
        model.addAttribute("cname",classes);
        model.addAttribute("bj",3);
        return  "student/student";
    }


//    通过json传入到前端
    @ResponseBody
    @RequestMapping("/all/{id}")
    public CT selectByTid(@PathVariable int id, Model model){
        CT ct = ctService.selectCT(id);
        return ct;
    }


//    --------------------class班级----------------

    @RequestMapping(value = "/toClass")
    public String  toClass(int id, Model model, HttpSession session){
        List<Class> classes = ctService.selectCname(null);
        model.addAttribute("cname",classes);
        return "teacher/class";
    }


//根据班级查询学生
    @RequestMapping("/selStuByCid")
    public String selectStuByCid(@RequestParam(required = true,value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(required = true,value = "pageSize",defaultValue = "5")Integer pageSize,
                                  int id, Model model){
        List<Class> classes = ctService.selectCname(null);
        PageInfo<Student> students = studentService.selectStuAll(pageNum,pageSize,Arrays.asList(id));
        System.out.println(students);
        model.addAttribute("pageInfoL",students);
        model.addAttribute("cname",classes);
        model.addAttribute("bj",2);
        model.addAttribute("cid",id);
        System.out.println(classes);
        System.out.println(id);
        System.out.println(students);
        //        设置标记
        return "teacher/class";
    }


}
